package com.haoshuang.zhouzhoubang.ui.fragment

import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.FragmentMemberGroupBinding
import com.haoshuang.zhouzhoubang.model.bean.TeamGroupBean
import com.haoshuang.zhouzhoubang.ui.adapter.MemberGroupAdapter
import com.haoshuang.zhouzhoubang.ui.vm.MemberGroupViewModel
import com.lnkj.jialubao.utils.AccountUtils
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.core.finishAll
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.vertical


/**
 *  分组管理
 */
class MemberGroupFragment : BaseVMFragment<MemberGroupViewModel, FragmentMemberGroupBinding>() {

    companion object {
        fun getInstance(): MemberGroupFragment {
            val fragment = MemberGroupFragment()
            fragment.arguments = Bundle().apply {
                
            }
            return fragment
        }
    }

    private val data = arrayListOf<TeamGroupBean>()
    private lateinit var adapter: MemberGroupAdapter

    override fun initView(savedInstanceState: Bundle?) {

        adapter = MemberGroupAdapter(data, vm)
        binding.recyclerView.vertical()
            .adapter = adapter

    }

    override fun initData() {
        binding.refreshLayout.setOnRefreshListener {
            isShowing = false
            vm.getTeamGroup()
        }

    }

    override fun lazyLoadData() {

    }

    override fun onResume() {
        super.onResume()
        vm.getTeamGroup()
    }

    override fun startObserve() {
        vm.getTeamGroupData.observeState(this){
            onLoading { showLoading()}
            onError {code, msg ->
                dismissLoading()
                binding.refreshLayout.finishAll()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                val idList = AccountUtils.teamGroupShowList
                val list = it ?: mutableListOf()
                list.forEach {
                    it.isShow = idList.contains(it.id.toString())
                }
                data.clear()
                data.addAll(list)
                adapter.notifyDataSetChanged()
                binding.refreshLayout.finishAll()
            }
        }
    }

}